﻿package game 
{
	import flash.events.EventDispatcher;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.filters.ConvolutionFilter;
	import game.events.EventData;
	/**
	 * ...
	 * @author Doan Nguyen Thuy Nha Truc (Bamboo)	
	 */
	public class VideoControl extends EventDispatcher
	{
		private var _flvControl:Video;
		public function VideoControl() 
		{
			_flvControl = new Video();
		}
		/**
		 * @author	Bamboo
		 * @param	flvSource
		 */
		
/*		public function updateVideoPath(flvSource:String):void {
			try{
				if(flvControl!=null)
				{	
					this.removeChild(flvControl);
					flvControl = null;
				}				
				flvControl = new FLVPlayback(); 
				flvControl.x = 3; 
				flvControl.y = 30;      
				flvControl.width = 260;
				flvControl.height = 195;
				flvControl.isLive = true;
				flvControl.source = flvSource;				
				flvControl.getVideoPlayer(flvControl.activeVideoPlayerIndex).smoothing = true;				 
				var filter:ConvolutionFilter = new ConvolutionFilter();
				filter.matrixX = 0;
				filter.matrixY = 0;
				filter.matrix = [-1, 0, -1, 0, 8, 0, -1, 0, -1];
				filter.bias =  0;
				filter.divisor = 4;
				flvControl.filters = [filter];
				addChild(flvControl);
				var otherindex = getChildIndex(flvControl);
				
				setChildIndex(flvControl, 1);
				setChildIndex(bgMovie_mc, 0);
				
		    }catch (e:Error) 
		    {
				trace(e.message);
		    }
		}*/
		
		public var connect_nc:NetConnection;
		public var stream_ns:NetStream;
		public var connection:String = "rtmp://113.21.208.1/ipTV";
		public var streamName:String;
		private var clientObjectVdo:Object;
		public function updateVideoPath(fms:String, flvControl:Video, _x:int, _y:int, _width:int,_height:int ):Video {
			try {
				
				_flvControl = flvControl;
				
				var temp:Array = fms.split("@");
				connection = temp[0];
				streamName = temp[1];
				flvControl = new Video();
				flvControl.x = 270; 
				flvControl.y = 60.4;      
				flvControl.width = 432;
				flvControl.height = 320;
				flvControl.smoothing = true;
				var filter:ConvolutionFilter = new ConvolutionFilter();
				filter.matrixX = 0;
				filter.matrixY = 0;
				filter.matrix = [-1, 0, -1, 0, 8, 0, -1, 0, -1];
				filter.bias =  0;
				filter.divisor = 4;
				flvControl.filters = [filter];
				clientObjectVdo = new Object();
				connect_nc = new NetConnection();
				connect_nc.client = clientObjectVdo;

				connect_nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusNcIdHdl);
				connect_nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHdl);
				connect_nc.connect(connection);
					
		    }catch (e:Error) 
		    {
				trace("ms error" + e.message);
		    }
			return flvControl;
		}
			
		private function netStatusNcIdHdl(e:NetStatusEvent):void {		
			trace("netStatusNcIdHdl" + e.info.code);
			switch(e.info.code) {
				case "NetConnection.Connect.Success":
					stream_ns = new NetStream(connect_nc);
					dispatchEvent( new EventData( EventData.PLAY_VIDEO, true));
					break;
				case "NetConnection.Connect.Failed":
					break;
				case "NetConnection.Connect.Rejected":
					break;
				case "NetConnection.Connect.Closed":
						break;
			}
		}	
		
		/**
		 * @author Bamboo
		 */
		private function securityErrorHdl(e:SecurityErrorEvent):void {
			trace("SecurityErrorHdl: " + e);
		}	
		
		
		/**
		 * @author Bamboo
		 */
		/*public function stopVideo():void {
			if (flvControl != null) {
				flvControl.stop();
				this.removeChild(flvControl);				
			}
		}*/
		
		public function stopVideo():void {
			if (_flvControl != null) {
				stream_ns.pause();
				_flvControl.parent.removeChild(_flvControl);
			}
		}
	}

}